package view;

import model.Borrower;
import model.LibrarySystem;
import model.Resource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Collection;

public class ReturnPanel extends JPanel {
    private final LibrarySystem system;
    private final MainFrame mainFrame;
    private JComboBox<Borrower> borrowerCombo;
    private JComboBox<Resource> resourceCombo;
    private JButton returnButton;
    private JLabel statusLabel;

    public ReturnPanel(LibrarySystem system, MainFrame mainFrame) {
        this.system = system;
        this.mainFrame = mainFrame;
        setLayout(new BorderLayout(10, 10));
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        initComponents();
    }

    private void initComponents() {
        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 标题
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        JLabel titleLabel = new JLabel("归还资源");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(new Color(25, 25, 112));
        mainPanel.add(titleLabel, gbc);

        // 借阅者选择
        gbc.gridy++;
        gbc.gridwidth = 1;
        mainPanel.add(new JLabel("选择借阅者:"), gbc);

        gbc.gridx = 1;
        borrowerCombo = new JComboBox<>();
        borrowerCombo.setRenderer(new BorrowerRenderer());
        borrowerCombo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        borrowerCombo.addActionListener(this::updateResourceList);
        mainPanel.add(borrowerCombo, gbc);

        // 资源选择
        gbc.gridx = 0;
        gbc.gridy++;
        mainPanel.add(new JLabel("选择待归还资源:"), gbc);

        gbc.gridx = 1;
        resourceCombo = new JComboBox<>();
        resourceCombo.setRenderer(new ResourceRenderer());
        resourceCombo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        mainPanel.add(resourceCombo, gbc);

        // 按钮区域
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

        returnButton = new JButton("确认归还");
        returnButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        returnButton.setBackground(new Color(70, 130, 180));
        returnButton.setForeground(Color.WHITE);
        returnButton.addActionListener(this::returnResource);

        JButton toBorrowBtn = new JButton("借阅资源");
        toBorrowBtn.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        toBorrowBtn.addActionListener(e -> mainFrame.showPanel("borrow"));

        buttonPanel.add(returnButton);
        buttonPanel.add(toBorrowBtn);

        mainPanel.add(buttonPanel, gbc);

        // 状态标签
        gbc.gridy++;
        statusLabel = new JLabel(" ", JLabel.CENTER);
        statusLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        mainPanel.add(statusLabel, gbc);

        add(mainPanel, BorderLayout.CENTER);

        // 刷新数据
        refresh();
    }

    private void updateResourceList(ActionEvent e) {
        resourceCombo.removeAllItems();
        Borrower borrower = (Borrower) borrowerCombo.getSelectedItem();
        if (borrower != null) {
            for (Resource res : borrower.getBorrowedResources().values()) {
                resourceCombo.addItem(res);
            }

            if (resourceCombo.getItemCount() > 0) {
                resourceCombo.setSelectedIndex(0);
            }
        }
    }

    private void returnResource(ActionEvent e) {
        Borrower borrower = (Borrower) borrowerCombo.getSelectedItem();
        Resource resource = (Resource) resourceCombo.getSelectedItem();

        if (borrower == null || resource == null) {
            showStatus("请选择借阅者和资源", Color.RED);
            return;
        }

        if (system.returnResource(borrower.getId(), resource.getId())) {
            showStatus("归还成功! 资源ID: " + resource.getId(), Color.BLUE);
            refresh();
        } else {
            showStatus("归还失败: 该资源不属于此借阅者", Color.RED);
        }
    }

    private void showStatus(String message, Color color) {
        statusLabel.setText(message);
        statusLabel.setForeground(color);
    }

    public void refresh() {
        // 更新借阅者列表
        borrowerCombo.removeAllItems();
        for (Borrower borrower : system.getAllBorrowers()) {
            borrowerCombo.addItem(borrower);
        }

        // 更新资源列表
        updateResourceList(null);

        showStatus("准备就绪，请选择借阅者及其待归还资源", Color.DARK_GRAY);
    }

    // 自定义渲染器（复用BorrowPanel中的实现）
    private static class BorrowerRenderer extends BorrowPanel.BorrowerRenderer {}
    private static class ResourceRenderer extends BorrowPanel.ResourceRenderer {}
}